package com.haidechizi.sort;

import java.util.Arrays;

/**
 * @author: wangdejun
 * @create: 2020-04-14 10:19
 * 时间复杂度：O(n^2)
 * 空间复杂度：O(1)
 **/
public class SelectSort {

    public static void main(String[] args) {
        int[] array = new int[]{9, 3, 17, 8, 5, 6, 7, 3, 5, 6, 2, 1, 33};
        selectSort(array);
        System.out.println(Arrays.toString(array));

    }

    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int min = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[min] > array[j]) {
                    min = j;
                }
            }
            if (min != i) {
                int temp = array[min];
                array[min] = array[i];
                array[i] = temp;
            }
        }
    }
}
